﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC3DEMO.Models;
using MVC3DEMO.Models.BS;

namespace MVC3DEMO.Controllers
{
    public class MenuItemController : Controller
    {
        //
        // GET: /MenuItem/
        private SQLHeperDataContext sqlheper;
        private BS_MenuItem bs_menuItem;
        private BS_Menu bs_menu;
        public MenuItemController()
        {
            sqlheper = new SQLHeperDataContext();
            bs_menuItem = new BS_MenuItem();
            bs_menu = new BS_Menu();
        }
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /MenuItem/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /MenuItem/Create

        public ActionResult Create()
        {
            try
            {

                ViewBag.MenuId = new SelectList(sqlheper.Menus, "Id", "MenuName");

                var listMenu = bs_menu.getListMenu();
                ViewData["listMenu"] = listMenu;
                return View();
            }
            catch
            {
               
                return View();
            }
           
        } 

        //
        // POST: /MenuItem/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                var menuItem = new MenuItem();
                menuItem.MenuItemName = collection["MenuItemName"];
                menuItem.priority = int.Parse(collection["priority"].ToString().Trim());
                menuItem.MenuId = int.Parse(collection["MenuId"].ToString().Trim());
                var show = collection["show"];
                if (show == "false")
                {
                    menuItem.show = false;
                }
                else
                {
                    menuItem.show = true;
                }
                sqlheper.asp_InsertMenuItem(menuItem.MenuItemName, menuItem.priority,menuItem.show,menuItem.MenuId);
                return RedirectToAction("Create");
            }
            catch
            {
                ViewBag.MenuId = new SelectList(sqlheper.Menus, "Id", "MenuName");
                return View();
            }
        }
        
        //
        // GET: /MenuItem/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /MenuItem/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /MenuItem/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /MenuItem/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
